package com.jwthhealth_pub.old.subscribe.address;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.common.Address.Address;
import com.jwthhealth_pub.old.common.Address.AddressDao;
import com.jwthhealth_pub.old.common.Constant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * choose province
 * Created by Administrator on 2017/7/18.
 */

public class SubscribeProvinceActivity extends BaseActivity {
    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.province_list)
    RecyclerView provinceList;

    private AddressDao mAddressDao = AddressDao.getInstance();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe_provinces);
        ButterKnife.bind(this);

        List<Address> provinces = mAddressDao.getProvinces();
        provinceList.setLayoutManager(new LinearLayoutManager(this));
        provinceList.setAdapter(new ProvinceAdapter(provinces));
    }

    @Override
    protected void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    private class ProvinceAdapter extends RecyclerView.Adapter {
        private List<Address> provinces;

        private ProvinceAdapter(List<Address> provinces) {
            this.provinces = provinces;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(View.inflate(SubscribeProvinceActivity.this, R.layout.item_address_subscribe, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder vh = (ViewHolder) holder;
            final int pos = vh.getAdapterPosition();
            vh.name.setText(provinces.get(position).getName());

            vh.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(SubscribeProvinceActivity.this, SubscribeCityActivity.class);
                    Address address = provinces.get(pos);
                    intent.putExtra(Constant.PROVINCEID, address.getSpid());
                    intent.putExtra(Constant.PROVINCENAME, address.getName());
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            if (provinces != null)
                return provinces.size();
            else
                return 0;
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            public TextView name;

            public ViewHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.text);
            }
        }
    }
}
